var delicious
var subPub
var getDeliciousBookmarSystem
var loginId
var editJob
var bookmarkStorage
var Job;
function installTag(tag) {
	var ic = $$('#tags-n-trees .input-item.result')[0];

	var div = document.createElement('div');
	div.innerHTML = tag.tag + ' : ' + tag.count;
	div.className = "result-item";
	ic.appendChild(div);

	div = document.createElement('div');
	div.className = "hr";
	ic.appendChild(div);
}

var deliciousIcon = document.createElement('img');
deliciousIcon.src = '../images/delicious.36.gif';

function acceptTag(event) {
	if (event.dataTransfer.getData('tag')) {
		event.preventDefault();
		return false;
	} else {
		event.preventDefault();
	}
}

function cancel(event) {
	event.preventDefault();
	return false;
}
function installBookmark(bookmark) {
	var ic = $$('#bookmarks .input-item.result')[0];
	var isFirst = $$('#bookmarks .input-item.result div.result-item').length == 0;
	if (!isFirst) {
		var div = document.createElement('div');
		div.className = "hr";
		ic.appendChild(div);
	}
	{
		var div = document.createElement('div');
		var html = [];

		html.push('<div class="title" draggable="false"><a href="'
				+ bookmark.url + '" target="blank">' + bookmark.name
				+ '</a></div>');

		html.push('via <a class="href" draggable="false" href="' + bookmark.url
				+ '" target="blank">' + bookmark.url + '</a>');

		html.push('<div class="tags">');
		if (bookmark.tags && bookmark.tags.length) {
			bookmark.tags.each(function(tag) {
						html.push('<div>');
						html.push(tag);
						html.push('</div>');
					});
		} else {
			html.push('Not tagged!');
		}
		html.push('</div>');

		div.innerHTML = html.join(' ');

		div.className = isFirst ? "result-item first" : "result-item";
		div.bookmark = bookmark;
		ic.appendChild(div);
		div.addEventListener('dragstart', function(event) {
					// event.preventDefault();
					div.addClassName('dragging');
					console.dir(bookmark);
					event.dataTransfer.effectAllowed = 'copy';
					event.dataTransfer.setData("text/html", Object
									.toJSON(bookmark));
					// event.dataTransfer.setData("text/uri-list",
					// bookmark.url);
					event.dataTransfer.setData("text/plain", bookmark.name);
					event.dataTransfer.setDragImage(div, -10, -10);
					return false;
				}, false);
		div.addEventListener('dragenter', cancel, false);
		div.addEventListener('dragover', acceptTag, false);
		div.addEventListener('drop', function(event) {
					event.preventDefault();
					if (event.dataTransfer.types)
						event.dataTransfer.types.each(function(type) {
									console.log(type + "="
											+ event.dataTransfer.getData(type));
								})

					return false;
				}, false);
		div.draggable = true;
		div.setAttribute('aria-dropeffect', "copy move");
	}
}

function update_now() {
	delicious.startRequest();
}

function togglePanel(id) {
	$(id).toggleClassName('hover');
}

function openPanel(id) {
	$(id).addClassName('hover');
}

function closePanel(id) {
	$(id).removeClassName('hover');
}

function loadResults() {
	$$('#bookmarks .input-item.result')[0].innerHTML = '';
	$$('#tags-n-trees .input-item.result')[0].innerHTML = '';

	var cache = bookmarkStorage.getCache();
	var tags = [];
	var tagsMap = {};
	for (var md5 in cache) {
		var bm = cache[md5];
		if (bm.name && bm.url) {
			installBookmark(bm);
			if (bm.tags) {
				bm.tags.each(function(tag) {
							if (!tagsMap[tag]) {
								tagsMap[tag] = [];
								tags.push(tag);
							}
							tagsMap[tag].push(bm);
						});
			}
			$('bookmarks').removeClassName('disabled');
		}
	}

	var maxUsed = 0;
	var sortBucket = {};
	tags.sort();
	tags.each(function(tag) {
				if (tagsMap[tag].length) {
					if (maxUsed < tagsMap[tag].length)
						maxUsed = tagsMap[tag].length;
					if (!sortBucket[tagsMap[tag].length])
						sortBucket[tagsMap[tag].length] = [];
					sortBucket[tagsMap[tag].length].push(tag);
				}
			});
	var sorted = [];
	for (var i = maxUsed; i >= 0; i--) {
		if (sortBucket[i])
			sortBucket[i].each(function(tag) {
						installTag({
									tag : tag,
									count : i
								});
						$('tags-n-trees').removeClassName('disabled');
					});
	}
}

window.addEventListener('load', function() {
	delicious = chrome.extension.getBackgroundPage().delicious;
	subPub = chrome.extension.getBackgroundPage().subPub;
	getDeliciousBookmarSystem = chrome.extension.getBackgroundPage().getDeliciousBookmarSystem;
	bookmarkStorage = chrome.extension.getBackgroundPage().bookmarkStorage;
	Job = chrome.extension.getBackgroundPage().Job;
	document.body.appendChild(deliciousIcon);
}, false);